/*
 * @Author: Marte
 * @Date:   2016-10-27 14:25:34
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-03 16:35:13
 */

'use strict';

var currApp = angular.module('addpicremark.app', ['ui.bootstrap', 'ngFileUpload']);

currApp.controller('pagectrl', ['$scope', '$http', '$window', '$timeout', 'Upload',
    function($scope, $http, $window, $timeout, Upload) {
        var vm = this;

        vm.input = {
            picGroup: [{
                id: null,
                groupName: null,
                pic: [{
                    id: null,
                    fileName: null,
                    fileFullName: null
                }]
            }]
        };
        /*获取证件类型*/
        $http.get('/api/types')
            .success(function(data, status, headers, config) {
                vm.types = data;
            })
            .error(function(data, status, headers, config) {
                vm.types = null;
            });

        /*获取评语*/
        $http.get('/api/remarks')
            .success(function(data, status, headers, config) {
                vm.remarks = data;
            })
            .error(function(data, status, headers, config) {
                vm.remarks = null;
            });

        vm.addGroup = function() {
            vm.input.picGroup.push({
                groupId: null,
                groupName: null,
                pic: [{
                    id: null,
                    fileName: null,
                    fileFullName: null
                }]
            });
        };

        //保存表单
        vm.submit = function() {
            $http.post('/pictyperemarkgroups', {
                    input: vm.input
                })
                .success(function(data, status, headers, config) {
                    $window.location.replace('/');
                })
                .error(function(data, status, headers, config) {
                    alert(data.error ? data.error : '提交失败');
                });
        };

        //文件上传
        $scope.uploadFiles = function(files, picGroup) {
            var failFiles = '';
            if (files && files.length) {
                for (var i = 0; i < files.length; i++) {
                    //console.log(files[i]);
                    Upload.upload({
                            url: '/pics/uploadFiles',
                            data: { file: files[i] }
                        })
                        .success(function(data, status, headers, config) {
                            //console.log('data');
                            if (data.id) {
                                picGroup.pic[picGroup.pic.length - 1] = {
                                    id: data.id,
                                    fileName: data.fileName,
                                    fileFullName: data.fileFullName
                                };
                                picGroup.pic.push({});
                            } else {
                                failFiles += ((failFiles === '') ? '' : ',') + files[i].name;
                            }
                        })
                        .error(function(data, status, headers, config) {
                            failFiles += ((failFiles === '') ? '' : ',') + files[i].name;
                        });
                }
            }
            if (failFiles) {
                alert('以下文件上传失败\n' + failFiles);
            }
        };

        //删除文件
        $scope.deleteFile = function(picGroup, cindex) {
            $http({
                    method: 'DELETE',
                    url: '/pics/' + picGroup.pic[cindex].id
                })
                .success(function(data, status, headers, config) {
                    picGroup.pic.splice(cindex, 1);
                })
                .error(function(data, status, headers, config) {
                    alert(data.error ? data.error : '删除文件失败');
                });
        };

        //测试方法
        /*        vm.test = function() {
                    console.log(vm.input.picGroup);
                };*/

    }
]);
